<?php
/**
* product.php
*
* Create on 2017-2-17
* Create by TT
*/
namespace haibao\cms\business\newproduct;

use \haibao\cms\business\BusinessException;
use \haibao\cms\data\newproduct\Product as ProductData;

class Product extends \haibao\cms\business\Base{
	
	/**
	 * 添加产品与属性关系
	 * @param int $productId
	 * @param array(\haibao\cms\model\data\newproduct\ProductPropertyRelation) $models
	 */
	public static function addProductPropertyRelationModels($productId,$models){
		$data = new ProductData();
		$data->addProductPropertyRelationModels($productId,$models);
	}
	
	public static function getProduct($query){
		$data = new ProductData();
		return $data->getAll($query);
	}
	
	public static function getProductInfo($productId){
		if(empty($productId)){
			throw new BusinessException('产品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$productData = new ProductData();
		return $productData->getOneById($productId);
	}
	
	public static function getOneProductModel($query){
		$data = new ProductData();
		return $data->getOne($query);
	}
	
	public static function copyProduct($productId){
		$data = new ProductData();
		return $data->copyProduct($productId,self::getCurrentUser()->Id);
	}
	
	public static function addProduct($model,$brandIds,$categoryIdArr){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
	
		if(empty($model)){
			throw new BusinessException('产品不能为空',self::DATA_EMPTY);
		}
		if(empty($brandIds)){
			throw new BusinessException('产品所属品牌不能为空',self::DATA_EMPTY_ID);
		}
		if(empty($categoryIdArr)){
			throw new BusinessException('产品所属分类不能为空',self::DATA_ERROR_PRODUCT_CATEGORY_ID);
		}
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->CreateTime = new \DateTime();
		$productData = new ProductData();
		$productData->addProduct($model, $brandIds,$categoryIdArr);
	
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(true,true));
	}
	
	public static function updateProduct($id, $arrSet,$brandIds,$categoryIdArr){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($id)){
			throw new BusinessException('产品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		if(empty($brandIds)){
			throw new BusinessException('产品所属品牌不能为空',self::DATA_EMPTY_ID);
		}
		if(empty($arrSet)){
			throw new BusinessException('需要修改的数据不能为空',self::DATA_EMPTY);
		}
		$productData = new ProductData();
		$productData->updateProduct($id, $arrSet,$brandIds,$categoryIdArr);
		$arrSet['Id'] = $id;
		$arrSet['BrandIds'] = $brandIds;
		$arrSet['CategoryIds'] = $categoryIdArr;
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	public static function updateProductStatus($id,$status){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($id)){
			throw new BusinessException('单品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$productData = new ProductData();
		$productData->updateProductStatus($id, $status);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = $status;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	public static function getCategoryList(){
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		return $data->getCategoryList();
	}
	
	public static function getCategoryArr(){
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		return $data->getCategoryArr();
	}
	
	public static function getCategoryChildrenArr(){
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		return $data->getCategoryChildrenArr();
	}
	
	public static function getChildListByCategoryId($categoryId){
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		return $data->getChildListByCategoryId($categoryId);
	}
	
	public static function deleteCategory($categoryId){
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		return $data->deleteCategory($categoryId);
	}
	
	public static function addProductCategory($productCategoryModel){
		self::checkOperation(self::FUNC_CODE_PRODUCT_CATEGORY_ADD);
	
		if(!$productCategoryModel && !($productCategoryModel instanceof \haibao\cms\model\data\newproduct\ProductCategoryDefine)){
			throw new BusinessException('产品分类不存在',self::DATA_EMPTY);
		}
		if(!$productCategoryModel->CategoryName){
			throw new BusinessException('产品分类名称不能为空',self::DATA_EMPTY_NAME);
		}
		
		$productCategoryModel->CreateBy = self::getCurrentUser()->Id;
		$productCategoryModel->CreateTime = new \DateTime();
		$productCategoryModel->Status = \haibao\cms\model\data\newproduct\ProductCategoryDefine::STATUS_ENABLE;
	
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		$id = $data->addCategory($productCategoryModel);
	
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD, $productCategoryModel->toArray(true,true));
		return $id;
	}
	
	public static function saveProductCategory($productCategoryModel){
		self::checkOperation(self::FUNC_CODE_PRODUCT_CATEGORY_SAVE);
		
		if(!$productCategoryModel && !($productCategoryModel instanceof \haibao\cms\model\data\newproduct\ProductCategoryDefine)){
			throw new BusinessException('产品分类不存在');
		}
		if(!$productCategoryModel->CategoryName){
			throw new BusinessException('产品分类名称不能为空');
		}

		$data = new \haibao\cms\data\newproduct\ProductCategory();
		
		if($productCategoryModel->ParentIds){
			$categoryArr = $data->getCategoryArr();
			$categoryId = $productCategoryModel->Id;
			foreach($productCategoryModel->ParentIds as $parentId){
				//选择的上级分类的上级不能包含此分类
				foreach($categoryArr as $categorys){
					if($categorys['Id'] == $parentId){
						if(in_array($categoryId, $categorys['ParentIds'])){
							throw new BusinessException('上级分类选择错误');
						}
						break;
					}
				}
				//选择的上级分类的下级分类不能与此分类的下级有重合
				$currentChildrenIds = $data->getCategoryChildrenIds($categoryId);
				$parentChildrenIds = $data->getCategoryChildrenIds($parentId);
				
				if(array_intersect($currentChildrenIds, $parentChildrenIds)){
					throw new BusinessException('上级分类选择错误');
				}
			}
		}
		
		$data->saveCategory($productCategoryModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_CATEGORY_SAVE, $productCategoryModel->toArray(true,true));
	}
	
	/**
	 * 通过ID得分类实体
	 */
	public static function getCategoryById($categoryId){
		if(!$categoryId){
			throw new BusinessException('分类id不能为空');
		}
		$data = new \haibao\cms\data\newproduct\ProductCategory();
		return $data->getCategoryById($categoryId);
	}
	
	public static function getProductPropertyList(){
		$productData = new ProductData();
		return $productData->getProductPropertyList();
	}
	
	public static function addProductProperty($model){
		$productData = new ProductData();
		$productData->addProductProperty($model);
	}

}